//
//  DADownloadFile.m
//  DAKnowledge
//
//  Created by DreamArts on 16/7/29.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DADownloadFile.h"
#import "AFNetworking.h"
#import "DAFileUploadTableViewCell.h"
#import "UAProgressView.h"
#import "DANetWorkReachabilityStatus.h"
#import "SVProgressHUD.h"


@implementation DADownloadFile

+ (void)downloadFileWithPath:(NSString *)path fileFolderName:(NSString *)fileFolderName FileExtend:(NSString *)fileExtend block:(ProgressBlock)blok commpleteBlock:(CommpleteBlock)commpleteBlock{
    // 文件保存路径路径
    NSArray *filePathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [filePathArray objectAtIndex:0];
    NSLog(@"文件保存路径: %@", documentDirectory);
    
    // 文件下载路径
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)path, NULL, NULL,  kCFStringEncodingUTF8 ));
    NSURL *url = [NSURL URLWithString:encodedString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *fullPath = [NSString stringWithFormat:@"%@/%@.%@", documentDirectory, fileFolderName, fileExtend];
    //判断网络连接
    //    if ([DANetWorkReachabilityStatus isNetWorkConnect]){
    //        NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //        NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
    //            // 正在下载
    //            CGFloat count = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
    //            NSLog(@"正在下载 %.2f", count);
    //
    //            blok(count);
    //        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
    //            return [NSURL fileURLWithPath:fullPath];
    //        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
    //            // 下载完成
    //            NSLog(@"下载完成");
    //            // 下载完成之后回调
    //            commpleteBlock();
    //
    //        }];
    //        [task resume];
    //        }else{
    //            NSLog(@"无网络");
    //            NSString *str = NSLocalizedString(@"NoNetWorkCouldNotDownload", nil);
    //            [SVProgressHUD showErrorWithStatus:str];
    //        }
    //
    
    [DANetWorkReachabilityStatus isNetWorkConnectWithBlock:^(BOOL isConnect) {
        if (isConnect) {
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
                // 正在下载
                CGFloat count = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
                NSLog(@"正在下载 %.2f", count);
                
                blok(count);
            } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
                return [NSURL fileURLWithPath:fullPath];
            } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
                // 下载完成
                NSLog(@"下载完成");
                // 下载完成之后回调
              //  NSLog(@"========%@", response);
                commpleteBlock();
                
            }];
            [task resume];
        }else{
            NSLog(@"无网络");
            NSString *str = NSLocalizedString(@"NoNetWorkCouldNotDownload", nil);
            [SVProgressHUD showErrorWithStatus:str];
            //[[DANetWorkReachabilityStatus sharedManager] stopMonitoring];
        }
        
    }];

}

@end
